package com.carple.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carple.dao.MemberDao;
import com.carple.dao.RouteDao;
import com.carple.dto.Member;
import com.carple.dto.Route;

@WebServlet("/carsharing/carregister.action")
public class CarSharingRegisterServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// ///////////////////////////////////////////////////////////////////////
		String emailId = req.getParameter("emailid");
		String fee = req.getParameter("fee");
		String isMarried = req.getParameter("ismarried");

		Member member = new Member();
		member.setEmailId(emailId);
		member.setFee(fee);
		member.setIsMarried(isMarried);

		MemberDao memberDao = new MemberDao();
		memberDao.updateMember(member);
		// ///////////////////////////////////////////////////////////////////////

		RouteDao routeDao = new RouteDao();
		
		
		for (int i = 1; i < 6; i++) {
			if (req.getParameter("coordinates" + i) != null	&& req.getParameter("coordinates" + i).length() > 0) {
				String coordinates = req.getParameter("coordinates" + i);
				String routeorder = req.getParameter("routeorder" + i);
				
				String delims = "[,]";
				String[] tokens = coordinates.split(delims);
				String coordinatesX = tokens[0];
				String coordinatesY = tokens[1];

				Route route = new Route();// route는 DTO
				route.setCoordinates(coordinates);
				route.setCoordinatesX(coordinatesX);
				route.setCoordinatesY(coordinatesY);
				
				route.setRouteOrder(routeorder);
				route.setEmailId(emailId);
			
				routeDao.insertRouteDeparture(route);
				//routeDao.insertRouteDestination(route);
			}
		//////////////////////////////////////////////////////////////////	
		}

		resp.sendRedirect("/carple/index.jsp");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		doGet(req, resp);
	}
}
